//# sourceURL=http://uMapApp/Module/SotfRoad/SoftRoadModule
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../Module"
    , "../../UI/Panel/FloatPanel"
    , "../../UI/Panel/PanelBlock/ListBlock"
    , "../../UI/Panel/PanelBlock/PropertiesBlock"
    , "../../UI/Panel/Item/ListItem"
    , "../../UI/Layer"
    , "../../UI/Panel/PanelBlock/TreeBlock"
    , "../../UI/Tree/Tree"
    , "../StakeNum/Mileage"
    , "../Bridge/BridgeFiles"
    , "./SoftRoad"
], function (
    mhjs,
    uMap,
    uEarth,
    Module,
    FloatPanel,
    ListBlock,
    PropertiesBlock,
    ListItem,
    Layer,
    TreeBlock,
    Tree,
    Mileage,
    BridgeFiles,
    SoftRoad
) {

    function SoftRoadModule(option) {

        var _this = this;
        Module.call(_this, option);

        _this.title = "软基路段";
        _this.icon = _this.app.icons.roadLine;
        _this.panels = [];
        _this.items = [];

        _this.listPanel = new FloatPanel({"class": "rightPanel", "showClose": true});
        _this.listPanel.listBlock = new ListBlock({"class": "fullBlock"});
        _this.listPanel.listBlock.title = "软基路段列表";
        _this.listPanel.append(_this.listPanel.listBlock);

        _this.listPanel.on("close", function () {
            _this.closePanel();
        });

        _this.panels.push(_this.listPanel);

        _this.detailPanel = new FloatPanel({"class": "rightPanel", "showClose": true});
        _this.detailPanel.treeBlock = new TreeBlock({"class": "fullBlock"});
        _this.detailPanel.append(_this.detailPanel.treeBlock);

        _this.detailPanel.on("open", function () {
            _this.trigger("openDetail");
        });

        _this.detailPanel.on("close", function () {
            _this.trigger("closeDetail");
            _this.unActiveAllItem();
        });

        _this.panels.push(_this.detailPanel);


        _this.app.ui.layout.right.append(_this.listPanel);
        _this.app.ui.layout.right.append(_this.detailPanel);
    }

    var _class = SoftRoadModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "data"              : {
            set: function (data) {
                var _this = this;
                _this.clear();
                data && _this.renderList(data);
            },
            get: function () {
                return this._data;
            }
        },
        "renderList"        : {
            value: function (list) {

                var _this = this;

                $.each(list, function (i, item) {

                    if (_this.objMap) {
                        item = _this.objMap.format(item);
                    }

                    item.app = _this.app;
                    item.icon = _this.icon;

                    item.positions = item.positions[0];

                    var softRoad = new SoftRoad(item);
                    _this.items.push(softRoad);

                    softRoad.listItem = new ListItem({
                        "title"   : softRoad.title,
                        "subtitle": softRoad.subtitle,
                        "icon"    : _this.icon.base64,
                        "softRoad": softRoad
                    });


                    // softRoad.propertiesBlock = new PropertiesBlock();
                    // softRoad.propertiesBlock.title = softRoad.title;
                    // softRoad.propertiesBlock.map = _this.propertiesMap;
                    // softRoad.propertiesBlock.properties = softRoad;

                    softRoad.on("click", function (event) {
                        _this.activeItem(softRoad);
                        // _this.app.pop.target = softRoad;
                        // _this.app.pop.content = softRoad.propertiesBlock;
                        // _this.app.pop.position = event.position;
                        // _this.app.pop.open();

                        // _this.detailPanel.propertiesBlock.title = softRoad.title;
                        // _this.detailPanel.propertiesBlock.map = _this.propertiesMap;
                        // _this.detailPanel.propertiesBlock.content = softRoad;
                        // _this.detailPanel.open(false);

                    });

                    softRoad.on("close", function (softRoad) {
                        // if (_this.app.pop.target === softRoad) {
                        //     _this.app.pop.close();
                        // }
                    });

                    softRoad.on("unActive", function (softRoad) {
                        // if (_this.app.pop.target === softRoad) {
                        //     _this.app.pop.close();
                        // }
                    });

                    softRoad.open();

                    softRoad.listItem.on("click", function () {
                        _this.activeItem(softRoad);
                        softRoad.layer.active();

                        // _this.propertiesBlock.title = etc.title;
                        // _this.detailPanel.propertiesBlock.map = _this.propertiesMap;
                        // _this.detailPanel.propertiesBlock.properties = etc;
                        // _this.detailPanel.open();

                        // _this.app.servers.DC.requests.ETC_properties_map.send({}, function (ETC_map_json) {
                        //     _this.detailPanel.propertiesBlock.title = etc.name;
                        //     _this.detailPanel.propertiesBlock.map = ETC_map_json.data;
                        //     etc.dataObj.properties["编号"] = etc.num;
                        //     etc.dataObj.properties["名称"] = etc.name;
                        //     _this.detailPanel.propertiesBlock.properties = item.dataObj.properties;
                        //     _this.detailPanel.open();
                        // });
                    });

                    softRoad.listItem.on("hide", function (listItem) {
                        listItem.softRoad.close();
                    });

                    softRoad.listItem.on("show", function (listItem) {
                        listItem.softRoad.open();
                    });

                    _this.listPanel.listBlock.add(softRoad.listItem);

                });

                _this.listPanel.listBlock.filter = {
                    "param": "softRoad.searchKey"
                };

                _this.listPanel.listBlock.sorter = {
                    "param": {
                        "value": "softRoad.sortKey",
                        "label": "桩号"
                    }
                };
            }
        },
        "clear"             : {
            value: function () {
                var _this = this;

                $.each(_this.items, function (i, item) {
                    item.close();
                    item.listItem.remove();
                });

                _this._data = undefined;
                _this.items = [];
            }
        },
        "loadData"          : {
            value: function (data, callback) {

                var _this = this;
                if (_this.request) {
                    _this.request.send(data, function (json) {
                        if (json.features && json.features.length > 0) {
                            callback && callback(json.features);
                        }
                    });
                }
            }
        },
        "loadExtend"        : {
            value: function (data, callback) {

                var _this = this;
                if (_this.extendRequest) {
                    _this.extendRequest.send(data, function (json) {
                        if (json.data) {
                            callback && callback(json.data);
                        }
                    });
                } else {
                    callback && callback();
                }
            }
        },
        "open"              : {
            value: function (forceReload) {
                var _this = this;
                if (forceReload || !_this.isLoaded) {
                    _this.load({}, function (data, mapData) {

                        _this.loadExtend({}, function (extendJson) {

                            if (mapData) {
                                _this.objMap = mapData.obj;
                                _this.propertiesMap = mapData.properties;
                            }

                            if (extendJson) {
                                _this.extendData = extendJson;
                            }

                            _this.data = data;

                            _this.isLoaded = true;
                            _this.open();
                        });


                    });

                } else {
                    _this.isOpened = true;
                    $.each(_this.items, function (i, item) {
                        item.open();
                    });
                    _this.trigger("open");
                    _this.trigger("toggle");
                }
            }
        },
        "openPanel"         : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "close"             : {
            value: function () {
                var _this = this;
                _this.closePanel(false);
                _this.unActiveAllItem();
                $.each(_this.items, function (i, item) {
                    item.close();
                });
                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "activeItem"        : {
            value: function (item) {
                var _this = this;
                _this.unActiveAllItem();
                item.active();
                _this.openItemProperties(item);
                item.listItem.addClass("active");
                item.listItem.parent().animate({"scrollTop": item.listItem.dom[0].offsetTop});
            }
        },
        "openItemProperties": {
            value: function (item) {

                var _this = this;

                _this.detailPanel.treeBlock.title = item.title;

                if (!item.tree) {

                    item.tree = new Tree({
                        "list": []
                    });

                    var propertiseTree = new Tree({
                        "title": "路段属性"
                    });

                    item.tree.add(propertiseTree);
                    item.propertise = _this.propertiesMap.format(item);

                    $.each(item.propertise, function (context, value) {
                        propertiseTree.add({
                            "title": context + " : " + value
                        });
                    });

                    var documentTree = new Tree({
                        "title": "工程文档"
                    });

                    $.each(_this.extendData, function (context, list) {
                        var result = _this.searchSkateNumFile(item, list);

                        if (result.length > 0) {

                            var catalogTree = new Tree({
                                "title": context
                            });

                            $.each(result, function (i, files) {

                                var filesTree = new Tree({
                                    "title": files.title
                                });
                                $.each(files.attach, function (j, file) {

                                    var fileTree = filesTree.add(new Tree({
                                        "title": file.title
                                    }));

                                    file.modalityWindow = _this.app.modalityWindow;
                                    var bridgeFiles = new BridgeFiles(file);

                                    fileTree.ui.titleDiv.on("click", function () {
                                        bridgeFiles.openAttachment();
                                    });

                                });

                                if (filesTree.list) {
                                    catalogTree.add(filesTree);
                                }
                            });

                            if (catalogTree.list) {
                                documentTree.add(catalogTree);
                            }

                        }

                    });

                    if (documentTree.list) {
                        item.tree.add(documentTree);
                    }

                }

                _this.detailPanel.treeBlock.tree.clear();
                _this.detailPanel.treeBlock.tree.add(item.tree);
                _this.detailPanel.open(false);

            }
        },

        "searchSkateNumFile": {
            value: function (item, catalog) {
                var _this = this;

                var pattern = /(K|k)\d{1,}(\+\d{1,3})/g;
                var buildStakeNumPath = 61000;

                var result = [];

                $.each(catalog, function (i, files) {
                    if (pattern.test(files.title)) {

                        $.each(files.title.match(pattern), function (j, match) {
                            var stakeNum = new Mileage(match);
                            // stakeNum.distance = stakeNum.distance + buildStakeNumPath;
                            if (stakeNum.distance <= item.stopStakeNum.distance && stakeNum.distance >= item.startStakeNum.distance) {
                                result.push(files);
                            }
                        });
                    }
                });

                return result;

            }
        },
        "unActiveAllItem"   : {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.active(false);
                    item.listItem.removeClass("active");
                });
            }
        }
    });

    return _class;

});